//
//  LeftViewController.m
//  LoveListen
//
//  Created by 王喆 on 15/6/30.
//  Copyright (c) 2015年 王喆. All rights reserved.
//
#define kScreenSize [UIScreen mainScreen].bounds.size
#import "AFSoundManager.h"

#import "LeftViewController.h"
#import "DDMenuController.h"
#import "FirstViewController.h"
#import "AppDelegate.h"
#import "MusicViewController.h"
#import "SubscribeViewController.h"
#import "SettingViewController.h"
#import "LeftViewController.h"

@interface LeftViewController ()
{
    NSMutableArray *_titleArr;
    UIView * _view;
    BOOL  _isPlaying;
    UIImageView * _image;
    UILabel * _label;
    NSString * url;
    UIButton * _button;
    

}
@end

@implementation LeftViewController
@synthesize tableView = _tableView;

- (id)init {
    if ((self = [super init])) {
    }
    return self;
}


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
    return YES;
}


#pragma mark - View lifecycle





- (void)viewDidLoad {
    [super viewDidLoad];
    [self changeUIValue];
  //  [self creatLogo];
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:self.view.bounds];
    imageView.image =[UIImage imageNamed:@"backImg2"];
    [self.view addSubview:imageView];
    UIImageView *imageView1=  [[UIImageView alloc] initWithFrame:CGRectMake(10, 60, kScreenSize.width-150, 120)];
    imageView1.image = [UIImage imageNamed:@"leting"];
    [self.view addSubview:imageView1];
    
   // self.view.backgroundColor = [UIColor darkGrayColor];
    _titleArr = [[NSMutableArray alloc]initWithObjects:@"乐听·首页",@"乐听·书城",@"乐听·订阅",@"乐听·设置", nil];
    
    
    if (!_tableView) {
        UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, kScreenSize.height/3, kScreenSize.width, kScreenSize.height/3+20) style:UITableViewStylePlain];
        tableView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
        tableView.delegate = (id<UITableViewDelegate>)self;
        tableView.dataSource = (id<UITableViewDataSource>)self;
        tableView.backgroundColor = [UIColor clearColor];
        
        tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        tableView.tableHeaderView.backgroundColor = [UIColor clearColor];
       // [tableView setTintColor:[UIColor whiteColor]];
        
        
        tableView.bounces = NO;
        [self.view addSubview:tableView];
        self.tableView = tableView;
        
    }
    
}
-(void)changeUIValue{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playerControl:) name:@"change" object:nil];
}
//-(void)creatLogo{
//    UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(30, 100, 200, 100)];
//    view1.backgroundColor = [UIColor redColor];
//    
//    
//    
//    imageView.image = [UIImage imageNamed:@"sliderone"];
//    [view1 addSubview:imageView];
//    
//    [self.view addSubview:view1];
//    
//    
//
//
//}



-(void)playerControl:(NSNotification *)nf{
    if (_label) {
        [_label removeFromSuperview];
        [_image removeFromSuperview];
        [_button removeFromSuperview];
    }
    NSDictionary *user = nf.userInfo;
   // NSString *title = [user valueForKey:@"title"];
    NSString *title=user[@"title"];
    NSLog(@"%@",title);
    
    NSString *coverLager =[user valueForKey:@"coverLarge"];
    _isPlaying = [user valueForKey:@"isplay"];
    url = user[@"playUrl64"];
    if (title) {
        _view = [[UIView alloc]initWithFrame:CGRectMake(0, kScreenSize.height-60, kScreenSize.width, 60)];
        _view.backgroundColor = [UIColor clearColor];
        // view.backgroundColor  =  [UIColor colorWithRed:105/255.0 green:127/255.0 blue:156/255.0 alpha:1];
        _image = [[UIImageView alloc]initWithFrame:CGRectMake(10, 10, 40, 40)];
        _image.layer.masksToBounds = YES;
        _image.layer.cornerRadius = 5;
        [_image sd_setImageWithURL:[NSURL URLWithString:coverLager] placeholderImage:[UIImage imageNamed:@"3"]];
        _label  = [[UILabel alloc]initWithFrame:CGRectMake(60, 10, kScreenSize.width/2-60, 40)];
        _label.text = title;
        _label.textColor = [UIColor whiteColor];
        _label.font = [UIFont fontWithName:@"Yuppy SC" size:15];
        _label.numberOfLines = 0;
        [_view addSubview:_label];
        [_view addSubview:_image];
        
        _button = [UIButton buttonWithType:UIButtonTypeCustom];
        _button.frame = CGRectMake(kScreenSize.width/2+30, 15, 30, 30);
        if (_isPlaying ==YES) {
            [_button setImage:[UIImage imageNamed:@"tabbar_play_n"] forState:UIControlStateNormal];
        }else{
            [_button setImage:[UIImage imageNamed:@"tabbar_pause_n"] forState:UIControlStateNormal];
        }
        [_button addTarget:self action:@selector(playerClick:) forControlEvents:UIControlEventTouchUpInside];
        [_view addSubview:_button];
        
        [self.view addSubview:_view];
        
    }else{
        return;
    }
}

-(void)playerClick:(UIButton *)btn{
   // if ([[AFSoundManager sharedManager]audioPlayer]) {
        if (_isPlaying ==YES) {
            [[AFSoundManager sharedManager]pause];
            [btn setImage:[UIImage imageNamed:@"tabbar_play_n"] forState:UIControlStateNormal];
            _isPlaying = NO;
        }else{
            [self justPlay];
            [btn setImage:[UIImage imageNamed:@"tabbar_pause_n"] forState:UIControlStateNormal];
            _isPlaying = YES;
        }
    }
//else {
                   //  [self creatStreamer];
// [self justPlay];
                  //_isPlaying = NO;
//    }
    
    
//}
-(void)justPlay{
    UIBackgroundTaskIdentifier bgTask = 0;
    if([UIApplication sharedApplication].applicationState== UIApplicationStateBackground) {
        
        [[AFSoundManager sharedManager]resume];
        
        
        
        UIApplication*app = [UIApplication sharedApplication];
        
        UIBackgroundTaskIdentifier newTask = [app beginBackgroundTaskWithExpirationHandler:nil];
        
        if(bgTask!= UIBackgroundTaskInvalid) {
            
            [app endBackgroundTask: bgTask];
            
        }
        
        bgTask = newTask;
        
    }
    
    else {
       [[AFSoundManager sharedManager]resume];
        
    }
    
}

//-(void)creatStreamer{
//    if (_audioPlayer == nil) {
//        _audioPlayer = [[AudioPlayer alloc] init];
//    }
//    _audioPlayer.url = [NSURL URLWithString:url];
//}
//
//- (void)destroyStreamer
//{
//    if (_audioPlayer)
//    {
//        [_audioPlayer  stop];
//        _audioPlayer = nil;
//    }
//}






- (void)viewDidUnload {
    [super viewDidUnload];
    self.tableView = nil;
    
}

#pragma mark - UITableViewDataSource

- (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section {
    return _titleArr.count;
}

- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSArray *image = @[@"shouye",@"v_cert",@"walls_icon_rate@2x",@"walls_icon_appad@2x"];
    static NSString *CellIdentifier = @"CellIdentifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if(cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    
    
    UIImageView * image1 =[[UIImageView alloc]initWithFrame:CGRectMake(27, 9, 36, 36)];
    image1.image = [UIImage imageNamed:image[indexPath.row]];
    //cell.imageView.image = [UIImage imageNamed: @"article_navbar"];
    UILabel * label1 = [[UILabel alloc]initWithFrame:CGRectMake(80, 15, 120, 20)];
    label1.text = _titleArr[indexPath.row];
    label1.font = [UIFont fontWithName:@"Yuppy SC" size:22];
    label1.textColor = [UIColor whiteColor];
    [cell addSubview:image1];
    [cell addSubview:label1];
    //cell.textLabel.text = _title[indexPath.row];
    cell.backgroundColor = [UIColor clearColor];
    return cell;
    
}

//- (NSString*)tableView:(UITableView*)tableView titleForHeaderInSection:(NSInteger)section {
    
    
  //  return @"乐听电台";
//}


#pragma mark - UITableViewDelegate

- (void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSArray *arr = @[@"FirstViewController",@"ListenViewController",@"SubscribeViewController",@"SettingViewController"];
    
    
    // set the root controller
    DDMenuController *menuController = (DDMenuController*)((AppDelegate*)[[UIApplication sharedApplication] delegate]).menuController;
    Class cls = NSClassFromString(arr[indexPath.row]);
     FirstViewController  *controller = [[cls alloc] init];
    controller.title = _titleArr[indexPath.row];
    UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:controller];
    
    [menuController setRootController:navController animated:YES];
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    
}









- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
    
    
}



@end
